<template>
  <div class="center">
    <!-- 头部 -->
    <div class="table_header flex flex_bet flex-wrap">
      <div class="table_button">
        <el-button type="primary" icon="el-icon-refresh" size="mini" @click="Refresh"></el-button>
      </div>
      <div class="table_searchs flex">
        <div class="table_search flex">
          <el-input placeholder="搜索" v-model="searchTable" class="input-with-select" size="small">
            <el-select v-model="select" slot="prepend" placeholder="请选择" class="table_select" style="width:105px">
              <el-option label="用户名" value="1"></el-option>
              <el-option label="标题" value="2"></el-option>
              <el-option label="详情" value="3"></el-option>
            </el-select>
            <el-button slot="append" icon="el-icon-search" @click="idnameSearch"></el-button>
          </el-input>
        </div>
        <dynamicTable :checkboxVal="checkboxVal" @alter="alterCheckbok" />
      </div>
    </div>
    <!-- 设备归属管理table -->
    <el-table stripe style="width: 100%" :data="browserTable" :key="key" border fit highlight-current-row ref="tableDataRef">
      <el-table-column v-if="colData[0].istrue" min-width="110" align="center" prop="username" label="用户名"></el-table-column>
      <el-table-column v-if="colData[1].istrue" min-width="120" align="center" prop="remark" label="标题"> </el-table-column>
      <el-table-column v-if="colData[2].istrue" min-width="120" align="center" prop="ip" label="IP"></el-table-column>
      <el-table-column v-if="colData[3].istrue" min-width="160" align="center" prop="details" label="详情"></el-table-column>
      <el-table-column v-if="colData[4].istrue" min-width="140" align="center" prop="create_time" label="创建时间">
        <template slot-scope="scope">{{scope.row.create_time| newDate}}</template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="paging">
      <el-pagination :current-page.sync="listQuery.startSize" background @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-sizes="[10, 50, 100, 200]"
        layout="total, sizes, prev, pager, next, jumper" :total="total" :page-size="listQuery.pageSize">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import dynamicTable from "@/components/DynamicTable";
import objectable from "@/utils/objectable.js";
import { LOG } from "@/api/infos.js";
export default {
  components: { dynamicTable },
  data() {
    return {
      checkboxVal: ["用户名", "标题", "IP", "详情", "创建时间"],
      key: 1,
      colData: [], //动态控制table
      //归属table
      browserTable: [],
      total: 0,
      searchTable: "",
      select: "1", //搜索条件
      listQuery: { startSize: 1, pageSize: 10, userId: localStorage.getItem("ID"), username: undefined, remark: undefined, details: undefined }
    }
  },
  created() {
    this.colData = objectable(this.checkboxVal, this.checkboxVal); //转为数组对象，好控制table
    this.queryAdminLog();
  },
  methods: {
    queryAdminLog() {
      LOG(this.listQuery).then(res => {
        this.browserTable = res.data;
        this.total = res.total;
      }).catch(err => {
        this.browserTable = []
        this.total = 0
      });
    },
    //搜索input3: '',select: ''
    idnameSearch() {
      if (!this.select) return this.$message.warning("请先选择查询条件");
      this.listQuery.startSize = 1
      this.listQuery.pageSize = 10
      switch (this.select) {
        case '1':
          this.listQuery.username = this.searchTable
          break;
        case '2':
          this.listQuery.remark = this.searchTable
          break;
        case '3':
          this.listQuery.details = this.searchTable
          break;
      }
      this.queryAdminLog()
    },
    handleSizeChange(val) {
      this.listQuery.pageSize = val
      this.queryAdminLog();
    },
    handleCurrentChange(val) {
      this.listQuery.startSize = val
      this.queryAdminLog();
    },
    Refresh() {
      this.listQuery = { startSize: 1, pageSize: 10, userId: localStorage.getItem("ID"), username: undefined, remark: undefined, details: undefined }
      this.queryAdminLog()
    },
    //下拉动态table子组件
    alterCheckbok(key, conte) {
      this.key = key;
      this.colData.filter(i => {
        if (conte.indexOf(i.title) != -1) {
          i.istrue = false;
          this.$nextTick(() => {
            this.$refs.tableDataRef.doLayout();
          });
        } else {
          i.istrue = true;
          this.$nextTick(() => {
            this.$refs.tableDataRef.doLayout();
          });
        }
      });
    }
  }
}
</script>